package cn.school.controller.newExam;

import cn.school.common.api.CommonResult;
import cn.school.newExam.entity.ExamScore;
import cn.school.newExam.service.ExamScoreService;
import cn.school.newExam.utils.ExamScoreTable;
import cn.school.newExam.utils.ExcelApi;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/examScore")
public class ExamScoreController {

    @Resource
    private ExamScoreService examScoreService;

    @PostMapping("/initExamScoreTable")
    public CommonResult<ExamScoreTable> initExamScoreTable(@RequestBody ExamScoreTable examScoreTable){
        examScoreTable = examScoreService.initExamScoreTable(examScoreTable);
        return CommonResult.success(examScoreTable);
    }

    @PostMapping("/scoresRecord")
    public CommonResult scoresRecord(@RequestBody ExamScoreTable examScoreTable){
        return examScoreService.scoresRecord(examScoreTable);
    }

    @GetMapping("/getClassExamScoreCount")
    public CommonResult getClassExamScoreCount(@RequestParam String examPlanId,@RequestParam String classId,@RequestParam String subjectId){
        int count = examScoreService.getClassExamScoreCount(examPlanId,classId,subjectId);
        return CommonResult.success(count);
    }

    @PostMapping("/importScore")
    @CrossOrigin
    public CommonResult importScore(@RequestParam("file")MultipartFile file){
        return examScoreService.importScore(file) ? CommonResult.success() : CommonResult.failed("导入失败");
    }

    @GetMapping("/exportTemplate")
    public void exportTemplate(HttpServletResponse response){
        ExcelApi<ExamScore> excelApi = new ExcelApi<>(ExamScore.class);
        excelApi.exportTemplate(response,"学生成绩");
    }

    @PostMapping("/queryList")
    public CommonResult<List<ExamScore>> queryList(@RequestBody ExamScore dto){
        List<ExamScore> list = examScoreService.queryList(dto);
        return CommonResult.success(list);
    }
}
